<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('锜风公寓-维修列表')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>房号：</label>
                            <input type="text" name="roomNum"/>
                        </li>
                        <li>
                            月份：
                            <select name="queryYear" id="queryYear" class="form-control selectpicker " data-live-search="false" >
                                <!-- 动态生成年份 -->
                            </select>
                            <span class="select-span">年</span>
                            <select name="queryMonth" id="queryMonth" class="form-control selectpicker " data-live-search="false" >
                                <!-- 动态生成月份 -->
                            </select>
                            <span class="select-span">月</span>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="resetForm()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="qif:repair:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="qif:repair:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="qif:repair:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="qif:repair:import">
                <i class="fa fa-download"></i> 导入
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('qif:repair:edit')}]];
    var removeFlag = [[${@permission.hasPermi('qif:repair:remove')}]];
    var prefix = ctx + "qif/repair";
    var types = [[${@dict.getType('qif_room_unit')}]];
    function resetForm() {
        // 1. 若依表单重置（清空输入框、单选框等）
        $.form.reset();
        // 3. 重置年月选择器为当前年月
        const now = new Date();
        $('#queryYear').val(now.getFullYear()).selectpicker('refresh');
        $('#queryMonth').val(now.getMonth() + 1).selectpicker('refresh');
        $.table.search();
    }
    $(function () {
        // 初始化年份选择器（近5年，默认当前年）
        const now = new Date();
        const currentYear = now.getFullYear();
        const $year = $('#queryYear');
        for (let i = currentYear; i >= currentYear - 4; i--) {
            $year.append(`<option value="${i}">${i}</option>`);
        }
        $year.val(currentYear).selectpicker('refresh'); // 若依推荐用selectpicker刷新

        // 初始化月份选择器（1-12月，默认当前月）
        const $month = $('#queryMonth');
        for (let i = 1; i <= 12; i++) {
            const monthText = i.toString().padStart(2, '0'); // 补0显示（如01-12）
            $month.append(`<option value="${i}">${monthText}</option>`);
        }
        $month.val(now.getMonth() + 1).selectpicker('refresh');
        var options = {
            url: prefix + "/list",

            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            importUrl: prefix + "/importData",
            modalName: "锜风公寓-维修",
            columns: [{
                checkbox: true
            },
                {
                    field: 'repairId',
                    title: '维修id',
                    visible: false
                },
                {
                    field: 'unit',
                    title: '单元号',
                    align: 'center',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(types, value);
                    }
                },
                {
                    field: 'roomNum',
                    title: '房间号'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'repairDate',
                    title: '时间'
                },
                {
                    visible: editFlag == 'hidden' ? false : true,
                    title: '维修状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.repairId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.repairId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function statusTools(row) {
        if (row.status == 0) {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.repairId + '\')"></i> ';
        } else {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.repairId + '\')"></i> ';
        }
    }

    /* 用户管理-停用 */
    function disable(repairId) {
        $.modal.confirm("确认该事件未完成吗？", function() {
            $.operate.post(prefix + "/changeStatus", { "repairId": repairId, "status": 0 });
        })
    }

    /* 完成 */
    function enable(repairId) {
        $.modal.confirm("确认该事件已完成吗？", function() {
            $.operate.post(prefix + "/changeStatus", { "repairId": repairId, "status": 1 });
        })
    }
</script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果已经存在，更新这条数据。"> 是否更新已经存在的数据
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>